package com.amoby.base.commbank.reportfile;

import java.io.File;
import java.io.IOException;

/**
 * 文件解析程序
 * 
 */
public abstract class FileResolver {
    /**
     * 根据报告文件名的命名法则，自动产生相应的解析对象
     * 
     * @param fileName
     * @return
     */
    public static FileResolver build(String path, String fileName) {
        FileResolver resolver = null;
        int charIndex = fileName.lastIndexOf('.');
        if (charIndex == -1) {
            throw new UnsupportedOperationException("unsupport file format");
        }
        String name = fileName.substring(0, charIndex);
        String extension = fileName.substring(charIndex + 1);
        if ("csv".equalsIgnoreCase(extension)) {
            String csvType = null;
            String[] namePart = name.split("_");
            if (namePart.length == 5) {
                csvType = namePart[4];
                if ("CSV1".equalsIgnoreCase(csvType)) {
                    resolver = new TGEventsCSV1Resolver();
                } else if ("CSV2".equalsIgnoreCase(csvType)) {
                    throw new UnsupportedOperationException("unsupport CSV format 2");
                } else {
                    throw new UnsupportedOperationException("unsupport CSV format");
                }
            } else {
                resolver = new FileEventsCSVResolver();
            }
        } else if ("txt".equalsIgnoreCase(extension)) {
            // resolver = new EventsTXTResolver();
            throw new UnsupportedOperationException("deprecated txt file format");
        } else {
            throw new UnsupportedOperationException("unsupport file format");
        }
        return resolver;
    }
    protected ReportEvents reportEvents = null;

    /**
     * 执行解析
     * 
     * @param path
     * @throws IOException
     */
    public abstract ReportEvents resolver(File file) throws IOException;
}
